package com.sfdk.graphics.data
{
import com.sfdk.io.GenericIO;

import flash.display.BitmapData;
import flash.events.Event;
import flash.geom.Rectangle;
import flash.utils.ByteArray;

public class LayeredBitmap extends GenericIO
	{
		public function LayeredBitmap(layeredBitmapPath : String = null)
		{
			_layeredData = new Vector.<BitmapData>();
			if(layeredBitmapPath != null)
				super(layeredBitmapPath, GenericIO.DATA_FILE);
		}
		
		//--------------------------------------------------------------------------
		//
		//  properties
		//
		//--------------------------------------------------------------------------
		
		//----------------------------------
		// walkableData
		//----------------------------------
		
		/**
		 * @private
		 */
		private var _walkableData : Vector.<Vector.<int>>;

		public function get walkableData():Vector.<Vector.<int>>
		{
			return _walkableData;
		}

		public function set walkableData(value:Vector.<Vector.<int>>):void
		{
			_walkableData = value;
		}

		//----------------------------------
		// layeredData
		//----------------------------------
		
		/**
		 * @private
		 */
		private var _layeredData : Vector.<BitmapData>;

		public function get layeredData():Vector.<BitmapData>
		{
			return _layeredData;
		}

		public function set layeredData(value:Vector.<BitmapData>):void
		{
			_layeredData = value;
		}
		
		//----------------------------------
		// isLoaded
		//----------------------------------
		
		/**
		 * @private
		 */
		private var _isLoaded : Boolean;

		public function get isLoaded():Boolean
		{
			return _isLoaded;
		}

		public function set isLoaded(value:Boolean):void
		{
			_isLoaded = value;
		}
		
		//----------------------------------
		// width
		//----------------------------------
		
		/**
		 * @private
		 */
		private var _width : int;
		
		public function get width():int
		{
			return _width;
		}
		
		public function set width(value:int):void
		{
			_width = value;
		}
		
		//----------------------------------
		// height
		//----------------------------------
		
		/**
		 * @private
		 */
		private var _height : int;
		
		public function get height():int
		{
			return _height;
		}
		
		public function set height(value:int):void
		{
			_height = value;
		}
		
		//----------------------------------
		// width
		//----------------------------------
		
		/**
		 * @private
		 */
		private var _widthInTile : int;
		
		public function get widthInTile():int
		{
			return _widthInTile;
		}
		
		public function set widthInTile(value:int):void
		{
			_widthInTile = value;
		}
		
		//----------------------------------
		// heightInTile
		//----------------------------------
		
		/**
		 * @private
		 */
		private var _heightInTile : int;
		
		public function get heightInTile():int
		{
			return _heightInTile;
		}
		
		public function set heightInTile(value:int):void
		{
			_heightInTile = value;
		}
		
		
		// ------------------------------------------ //
		//											  //
		//   		  overriden methods				  //
		//											  //
		// ------------------------------------------ //
		
		override protected function init() : void
		{
			super.init();
			
			var byteArray : ByteArray = getByteData();
			byteArray.inflate();
			byteArray.position = 0;
			
			_width = byteArray.readUnsignedInt();
			_height = byteArray.readUnsignedInt();
			
			_widthInTile = _width / 24;
			_heightInTile = _height / 24;
			
			var layerIndex : int;
			for(layerIndex = 0; layerIndex < 7; layerIndex++) // 0 = Sol(2), 1 = Joueur, 2 = Upper(2)
			{
				var layerSize : int = byteArray.readUnsignedInt();
				
				var imageByteArray : ByteArray = new ByteArray();
				byteArray.readBytes(imageByteArray,0,layerSize);
				
				var imageBitmapData : BitmapData = new BitmapData(_width, _height);
				imageBitmapData.setPixels(new Rectangle(0,0,_width, _height), imageByteArray);
				_layeredData.push(imageBitmapData);
			}
			
			_walkableData = byteArray.readObject() as Vector.<Vector.<int>>;
			
			dispatchEvent(new Event("layerLoaded"));
			_isLoaded = true;
		}
	}
}